package com.xwj.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xwj.dao.RaUserDao;
import com.xwj.entity.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.xwj.service.Interface.RaUserService;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

/**
 * Created by thinkjoy on 2017/7/7.
 */
@Slf4j
@Service
@CacheConfig(cacheNames = "RaUserCache")
public class RaUserServiceImpl implements RaUserService {

    @Autowired
    private RaUserDao raUserDao;

    /**
     * 分页查询
     *
     * @param pageIndex
     * @param pageSize
     * @return
     */
    @Override
    @Cacheable(key = "#root.methodName+'_'+#pageIndex")
    public PageInfo<UserInfo> findAllUserPage(int pageIndex, int pageSize) {
        PageHelper.startPage(pageIndex, pageSize);
        List<UserInfo> userInfos = raUserDao.findAllUser();
        PageInfo<UserInfo> pageInfo = new PageInfo<>(userInfos);
        return pageInfo;
    }

    @Override
    @Cacheable(key = "#root.methodName+'_all'")
    public List<UserInfo> findAllUser() {
        try {
            return raUserDao.findAllUser();
        } catch (Exception e) {
            e.printStackTrace();
            return Collections.EMPTY_LIST;
        }
    }

    @Override
    @Cacheable(key = "#root.methodName+'_'+#accountID")
    public UserInfo findUserByAccountID(String accountID, String flag) {
        UserInfo userInfo;
        try {
            userInfo = raUserDao.findUserByAccountID(accountID, flag);
        } catch (Exception e) {
            log.error("RaUserService异常:", e);
            userInfo = null;
        }
        return userInfo;
    }

    @Override
    @Cacheable(key = "'findUserByAccountID_'+#accountID")
    public UserInfo findPropertyByAccountID(String accountID) {
        return raUserDao.findPropertyByAccountID(accountID);
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public int updateByPersonData(UserInfo userInfo) {
        int row = raUserDao.updateByPersonData(userInfo);
        return row;
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public int updatepwd(UserInfo userInfo) {
        int row = raUserDao.updatepwd(userInfo);
        return row;
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public int addSingleUser(UserInfo userInfo) {
        int row = raUserDao.addSingleUser(userInfo);
        return row;
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public int addMulUser(List<UserInfo> userInfos) {
        int row = raUserDao.addMulUser(userInfos);
        return row;
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public int delUserByAccountid(String accountID) {
        int row = raUserDao.delUserByAccountid(accountID);
        return row;
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public int updateDelfalgByAccountid(String accountID, String flag) {
        int row;
        if (flag.equals("0")) {
            row = raUserDao.updateDelfalgByAccountid(accountID, "1");
        } else {
            row = raUserDao.updateDelfalgByAccountid(accountID, "0");
        }
        return row;
    }

    @Override
    @CacheEvict(allEntries = true)
    @Transactional(isolation = Isolation.READ_COMMITTED)
    public int updateRoleWhenRoleDelete(int roleId) {
        try {
            return raUserDao.updateRoleWhenRoleDelete(roleId);
        } catch (Exception e) {
            log.error("RaUserService异常:", e);
            return 0;
        }

    }

    @Override
    @Cacheable(key = "#root.methodName+'_'+#roleId")
    public List<UserInfo> findUserByRoleId(int roleId) {
        try {
            return raUserDao.findUserByRoleId(roleId);
        } catch (Exception e) {
            log.error("RaUserService异常:", e);
            return Collections.emptyList();
        }

    }
}
